package javafx191labs.ssheypak.common.data;

import com.sun.tools.javafx.util.NotImplementedException;
import java.util.Map;
import java.util.HashMap;
import javafx191labs.ssheypak.common.exception.DataDuplicationException;
import javafx191labs.ssheypak.common.exception.DataInputException;

/**
 * Class designed for storing data in runtime.
 * Realization is not thread-safe.
 *
 * @author ssheipak
 */
public class InMemoryDataStore{

    /** Undelying collection for storing entities. */
    Map<Integer, Entity> map = new HashMap<Integer, Entity>();

    /**
     * Adds entity to storage.
     * @param entity is an instance of @Entity
     */
    public void add(Entity entity) throws DataDuplicationException, IllegalArgumentException{
        if(entity == null || entity.getId() == null){
            throw new IllegalArgumentException("Ошибка при сохранении сущности. Сущность не сохранена.");
        }
        
        if( !map.containsKey(entity.getId()) ){
            map.put(entity.getId(), entity);
            System.out.println("InMemoryDataStore.add -> entity["+entity+"] successfully saved.");
        }else{
            throw new DataDuplicationException("Сущность ["+entity.getDescription()+"] не сохранена, " +
                                               "т.к. идентификатор ["+entity.getId()+"] ");
        }
    }

    /**
     * Updates entity.
     * @param entity is a new entity value
     * @throws DataInputException if such entity is not in storage.
     * @throws IllegalArgumentException if entity doesn't fit requirements.
     *
     */
    public  void update(Entity entity) throws DataInputException, IllegalArgumentException{
        throw new NotImplementedException();
    }

    /**
     * Deletes entity.
     * @param entity is a new entity value.
     * @throws DataInputException if such entity is not in storage.
     * @throws IllegalArgumentException if entity doesn't fit requirements.
     *
     */
    public boolean delete(Entity entity) throws DataInputException, IllegalArgumentException{
        throw new NotImplementedException();
    }

}
